package com.canaan.business.service.inOutStock.impl;

import com.canaan.business.domain.inOutStock.StorehouseStockCheck;
import com.canaan.business.mapper.inOutStock.StorehouseStockCheckMapper;
import com.canaan.business.service.inOutStock.IStorehouseStockCheckService;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
 * 待检仓库存Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-09
 */
@Service
public class StorehouseStockCheckServiceImpl implements IStorehouseStockCheckService {
    @Resource
    private StorehouseStockCheckMapper storehouseStockCheckMapper;

    /**
     * 查询待检仓库存
     *
     * @param id 待检仓库存主键
     * @return 待检仓库存
     */
    @Override
    public StorehouseStockCheck selectStorehouseStockCheckById(Long id) {
        return storehouseStockCheckMapper.selectStorehouseStockCheckById(id);
    }

    /**
     * 查询待检仓库存列表
     *
     * @param storehouseStockCheck 待检仓库存
     * @return 待检仓库存
     */
    @Override
    public List<StorehouseStockCheck> selectStorehouseStockCheckList(StorehouseStockCheck storehouseStockCheck) {
        return storehouseStockCheckMapper.selectStorehouseStockCheckList(storehouseStockCheck);
    }

    /**
     * 新增待检仓库存
     *
     * @param storehouseStockCheck 待检仓库存
     * @return 结果
     */
    @Override
    public int insertStorehouseStockCheck(StorehouseStockCheck storehouseStockCheck) {
        if(StringUtils.isBlank(storehouseStockCheck.getNo())){
            storehouseStockCheck.setNo(TableNoUtil.getNo(StorehouseStockCheck.class));
        }
        storehouseStockCheck.setCreateBy(SecurityUtils.getUsername());
        storehouseStockCheck.setCreateTime(DateUtils.getNowDate());
        return storehouseStockCheckMapper.insertStorehouseStockCheck(storehouseStockCheck);
    }

    /**
     * 批量新增待检仓库存
     *
     * @param storehouseStockCheckList 待检仓库存列表
     * @return 结果
     */
    @Override
    public int batchInsertStorehouseStockCheck(List<StorehouseStockCheck> storehouseStockCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockCheck storehouseStockCheck : storehouseStockCheckList){
            if(StringUtils.isBlank(storehouseStockCheck.getNo())){
                storehouseStockCheck.setNo(TableNoUtil.getNo(StorehouseStockCheck.class));
            }
            storehouseStockCheck.setCreateBy(username);
            storehouseStockCheck.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<storehouseStockCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockCheckList.size()) {
                endIndex = storehouseStockCheckList.size();
            }
            insertNum = insertNum + storehouseStockCheckMapper.batchInsertStorehouseStockCheck(storehouseStockCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改待检仓库存
     *
     * @param storehouseStockCheck 待检仓库存
     * @return 结果
     */
    @Override
    public int updateStorehouseStockCheck(StorehouseStockCheck storehouseStockCheck) {
        storehouseStockCheck.setUpdateBy(SecurityUtils.getUsername());
        storehouseStockCheck.setUpdateTime(DateUtils.getNowDate());
        return storehouseStockCheckMapper.updateStorehouseStockCheck(storehouseStockCheck);
    }

    /**
     * 批量修改待检仓库存
     *
     * @param storehouseStockCheckList 待检仓库存列表
     * @return 结果
     */
    @Override
    public int batchUpdateStorehouseStockCheck(List<StorehouseStockCheck> storehouseStockCheckList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StorehouseStockCheck storehouseStockCheck : storehouseStockCheckList){
            storehouseStockCheck.setUpdateBy(username);
            storehouseStockCheck.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<storehouseStockCheckList.size();) {
            int endIndex = i+500;
            if (endIndex > storehouseStockCheckList.size()) {
                endIndex = storehouseStockCheckList.size();
            }
            updateNum = updateNum + storehouseStockCheckMapper.batchUpdateStorehouseStockCheck(storehouseStockCheckList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除待检仓库存
     *
     * @param ids 需要删除的待检仓库存主键集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockCheckByIds(Long[] ids) {
        return storehouseStockCheckMapper.deleteStorehouseStockCheckByIds(ids);
    }

    /**
     * 删除待检仓库存信息
     *
     * @param id 待检仓库存主键
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockCheckById(Long id) {
        return storehouseStockCheckMapper.deleteStorehouseStockCheckById(id);
    }

    /**
     * 查询待检仓库存
     *
     * @param no 编号
     * @return 待检仓库存
     */
    @Override
    public StorehouseStockCheck selectStorehouseStockCheckByNo(String no) {
        return storehouseStockCheckMapper.selectStorehouseStockCheckByNo(no);
    }
    /**
     * By条码查询待检仓库存
     * @param barcode
     * @return
     */
    @Override
    public StorehouseStockCheck selectStorehouseStockCheckByBarcode(String barcode) {
        StorehouseStockCheck check = this.storehouseStockCheckMapper.selectStorehouseStockCheckByBarcode(barcode);
        return check;
    }


    /**
     * 根据库位获取库存信息列表
     *
     * @param barcodes 库位列表
     * @return 库存
     */
    @Override
    public List<StorehouseStockCheck> getListByBarcodes(List<String> barcodes)
    {
        if(barcodes == null || barcodes.isEmpty()){
            return Collections.emptyList();
        }
        return this.storehouseStockCheckMapper.getListByBarcodes(barcodes);
    }

    /**
     * 批量删除待检仓库存
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockCheckByNos(String[] nos) {
        return storehouseStockCheckMapper.deleteStorehouseStockCheckByNos(nos);
    }

    /**
     * 删除待检仓库存信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStorehouseStockCheckByNo(String no) {
        return storehouseStockCheckMapper.deleteStorehouseStockCheckByNo(no);
    }


    /**
     * 根据质检任务编号修改状态
     *
     * @param taskNo 质检任务编号
     * @param status 状态
     */
    @Override
    public void updateStockStatusByTaskNo(String taskNo,String status){
        this.storehouseStockCheckMapper.updateStockStatusByTaskNo(taskNo,status);
    }

    @Override
    public List<StorehouseStockCheck> selectStorehouseStockCheckList2(StorehouseStockCheck storehouseStockCheck) {
        return storehouseStockCheckMapper.selectStorehouseStockCheckList2(storehouseStockCheck);
    }
}
